package com.example.smart.newsview;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.Xml;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.xmlpull.v1.XmlPullParser;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import android.test.AndroidTestCase;

public class MainActivity extends AppCompatActivity {
    private ListView lv;
    private List<News> news;
    private LinearLayout loading;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            loading.setVisibility(View.INVISIBLE);
            switch (msg.what){
                case 1:
                    lv.setAdapter(new NewsAdapter());
                    break;
                case 0:
                    Toast.makeText(MainActivity.this,"失败了",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv=(ListView)findViewById(R.id.lv);
        loading=(LinearLayout)findViewById(R.id.jz);
        loading.setVisibility(View.VISIBLE);
    new Thread(){
        @Override
        public void run() {
            try {
                news=NewsInfo.find(getResources().getString(R.string.serverpath));
                Message msg=Message.obtain();
                msg.what=1;
                handler.sendMessage(msg);

            } catch (Exception e) {
                Message msg=Message.obtain();
                msg.what=0;
                handler.sendMessage(msg);
                e.printStackTrace();
            }
        }
    }.start();

    }
    private class NewsAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return news.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View newView=View.inflate(MainActivity.this,R.layout.items,null);
            TextView tvtitle=newView.findViewById(R.id.title);
            TextView tvdescription=newView.findViewById(R.id.description);
            News item=news.get(i);
            tvtitle.setText(item.getTitle());
            tvdescription.setText(item.getDesciption());
//            TextView tv=new TextView(MainActivity.this);
//            tv.setText(news.get(i).toString());
//            return tv;
            return newView;
        }
    }
}
